package com.lanlengran.imagemake;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by ruiqin on 16-12-16.
 */

public class AboutFileUtil {
    public static boolean saveFile(String str, String fileNanme) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) { // SD卡根目录的hello.text
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "auais" + File.separator + fileNanme;
        } else  // 系统下载缓存根目录的hello.text
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "auais" + File.separator + fileNanme;

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(str.getBytes());
            outStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String readerFile(String fileNanme) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) { // SD卡根目录的hello.text
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "auais" + File.separator + fileNanme;
        } else  // 系统下载缓存根目录的hello.text
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "auais" + File.separator + fileNanme;

        try {
            File file = new File(filePath);
            FileInputStream fin = new FileInputStream(file);

            int length = fin.available();

            byte [] buffer = new byte[length];
            fin.read(buffer);

         //   String res = EncodingUtils.getString(buffer, "UTF-8");
            String res = new String(buffer);


            fin.close();
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static boolean saveSettingFile(String filePath, String str ) {
         try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }else {
                file.delete();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(str.getBytes());
            outStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String readerSettingForSDcard(String fileNanme) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) { // SD卡根目录的hello.text
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "auais" + File.separator + "DoorSetting"+ File.separator+fileNanme;
        } else  // 系统下载缓存根目录的hello.text{}
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "auais" + File.separator+ "DoorSetting"+ File.separator + fileNanme;

        try {
            File file = new File(filePath);
            FileInputStream fin = new FileInputStream(file);

            int length = fin.available();

            byte [] buffer = new byte[length];
            fin.read(buffer);

            //   String res = EncodingUtils.getString(buffer, "UTF-8");
            String res = new String(buffer);
//            File savaPath=new File(Environment.getExternalStorageDirectory().toString()+File.separator +"system" + File.separator + "model" + File.separator );
//            if (!savaPath.exists()){
//                savaPath.mkdirs();
//            }
//            File saveFile=new File(savaPath.getPath()+ File.separator + "MODEL");
//            FileOutputStream fos = new FileOutputStream(saveFile,true);
//            fos.write(res.getBytes());
//            fos.close();

            fin.close();
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
